<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;


class GroupMember extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%group_member}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public static function addData($data){
        $model = new static();
        if(!empty($data['member_id']))
            $model->member_id = $data['member_id'];
        if(!empty($data['group_id']))
            $model->group_id = $data['group_id'];
        return $model->save();
    }

    public static function quitGroup($data){
        $model = static::findOne($data);
        $model->deleted_at = time();
        return $model->save();
    }

    /**
     * @inheritdoc
     */
    public static function selectFieldData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('group_member');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['group_id'])){
            $query->andWhere(['group_id' => $data['group_id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function selectJoinData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('group_member');
        $query->leftJoin('member m','`group_member`.`member_id` = `m`.`id`');
        $query->where(['`group_member`.`deleted_at`' => null]);
        $query->orderBy([
            '`group_member`.`created_at`' => SORT_DESC,
        ]);
        if(!empty($data['group_id'])){
            $query->andWhere(['group_id' => $data['group_id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function selectLimitInfo($data = [],$field = '*',$limit = '')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('group_member');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['group_cat_id'])){
            $query->andWhere(['group_cat_id' => $data['group_cat_id']]);
        }
        if(!empty($limit)){
            $query->limit($limit);
        }
        $command = $query->createCommand();
        $result = $command->queryAll();
        foreach ($result as $k => $v){
            
        }
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function findJoinData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('group_member');
        $query->leftJoin('group m','`group_member`.`group_id` = `m`.`id`');
        $query->where(['`m`.`deleted_at`' => null]);

        if(!empty($data['group_id'])){
            $query->andWhere(['group_id' => $data['group_id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryOne();
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function findData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('group_member');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['group_id'])){
            $query->andWhere(['group_id' => $data['group_id']]);
        }
        if(!empty($data['member_id'])){
            $query->andWhere(['member_id' => $data['member_id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryOne();
        return $result;
    }


}
